module D{
    export class Table{
        /** 桌子ID */
        public tableID:number;
        /** 玩家列表 */
        public players:Array<PlayerData>;
        /** 游戏状态 */
        public gameStatus:number;
        /** 等待准备秒数 -1特殊值不需要自动准备 */
        public waitReady:number;
        /** 等待定时器 */
        private wait:number;

        /** 构造函数 */
        public constructor(){
            this.tableID = 65535;
            this.gameStatus = 0;
            this.waitReady = 0;
            this.players = new Array<PlayerData>();
        }

        /** 玩家进入 */
        public playerEnter(e:egret.Event):number{
            let d = e.data;
            let pd:PlayerData;
            if(d.U == App.userData.userID) {
                pd = App.userData;
                this.tableID = d.T;
                if(d.S == UserStatus.PLAY) this.waitReady = 8;
            } else pd = new PlayerData();
            pd.faceID = d.F;
            pd.userID = d.U;
            pd.gender = d.G;
            pd.chairID = d.C;
            pd.userStatus = d.S;
            pd.score = d.M;
            pd.nickName = d.A;
            if(pd.chairID != 65535) this.players[pd.chairID] = pd;
            else {
                for(let i=0;i<this.players.length;i++){
                    if(this.players[i] && this.players[i].userID == pd.userID){
                        delete this.players[i];
                        return i;
                    }
                }
            }
            return pd.chairID;
        }

        /** 玩家的游戏币变化 */
        public playerScore(e:egret.Event):void{
            let d = e.data;
            for(let i=0;i<this.players.length;i++){ //当前用UserID判断其实可以直接用椅子号 待优化
                if(this.players[i] && this.players[i].userID == d.U){ 
                    this.players[i].score = d.M;
                    break;
                }
            }
        }

        /** 玩家状态变化 */
        public playerStatus(e:egret.Event):number{
            let d = e.data, cid = 65535;
            let pd:PlayerData;
            if(this.players[d.C]) pd = this.players[d.C];
            else{
                if(d.U == App.userData.userID) pd = App.userData;
                else{
                    for(let i=0;i<this.players.length;i++){
                        if(this.players[i] && this.players[i].userID == d.U) pd = this.players[i];
                    }
                }
                if(d.C == 65535) delete this.players[pd.chairID];
            }
            cid = pd.chairID;
            pd.chairID = d.C;
            pd.userStatus = d.S;
            if(App.userData.userStatus == UserStatus.NULL){
                this.players = [];
                this.waitReady = 0;
                if(this.wait) egret.clearTimeout(this.wait);
                this.wait = null;
            }else if(d.U == App.userData.userID && App.userData.userStatus == UserStatus.SIT){
                console.log("this.waitReady", this.waitReady);
                if(this.waitReady >= 0){
                    this.wait = egret.setTimeout(this.sendReady, this, this.waitReady * 1000);
                }
            }
            return cid;
        }
        /** 游戏状态 */
        public gameConfig(e:egret.Event):void{
            this.gameStatus = e.data.bGameStatus;
        }
        /** 发送准备 */
        public sendReady():void{
            if(this.waitReady == 0){
                WebSocketInstance.getHallSocket().send(CMDConfig.SEND_SYNTABLE, { bAllowLookon: 1 });
                this.waitReady = 8;
            } 
            if(App.userData.userStatus == UserStatus.SIT) WebSocketInstance.getHallSocket().send(CMDConfig.SEND_READY);
        }
    }
}